export var rules = {
    tag: [{ required: true, message: "请选择标签", trigger: "blur" }],
    title: [{ required: true, message: "请输入标题", trigger: "change" }],
    content: [
        { required: true, message: "请输入文章内容", trigger: "blur" },
    ],
}


export var editorOption = {
    placeholder: "编辑文章内容",
    modules: {
        toolbar: [
            ["bold", "italic", "underline", "strike"], //加粗，斜体，下划线，删除线
            ["blockquote", "code-block"], //引用，代码块
            [{ header: 1 }, { header: 2 }], // 标题，键值对的形式；1、2表示字体大小
            [{ list: "ordered" }, { list: "bullet" }], //列表
            // [{ script: "sub" }, { script: "super" }], // 上下标
            [{ indent: "-1" }, { indent: "+1" }], // 缩进
            // [{ direction: "rtl" }], // 文本方向
            // [{ size: ["small", false, "large", "huge"] }], // 字体大小
            // [{ header: [1, 2, 3, 4, 5, 6, false] }], //几级标题
            [{ color: [] }, { background: [] }], // 字体颜色，字体背景颜色
            // [{ font: [] }], //字体
            [{ align: [] }], //对齐方式
            ["clean"], //清除字体样式
            ["link", "image"], //上传图片、上传视频
        ],
    },
}

const uploadConfig = {
    action: "http://47.103.69.233:8199/fastdfs/uploadFile", // 必填参数 图片上传地址
    methods: "POST", // 必填参数 图片上传方式
    token: "", // 可选参数 如果需要token验证，假设你的token有存放在sessionStorage
    name: "file", // 必填参数 文件的参数名
    size: 1024, // 可选参数   图片大小，单位为Kb, 1M = 1024Kb
    accept: "image/png, image/gif, image/jpeg, image/bmp, image/x-icon", // 可选 可上传的图片格式
};
export const handlers = {
    image: function image() {
        let self = this;
        var fileInput = this.container.querySelector("input.ql-image[type=file]");
        if (fileInput === null) {
            fileInput = document.createElement("input");
            fileInput.setAttribute("type", "file");
            // 可设置上传图片的格式
            fileInput.setAttribute("accept", "image/png, image/gif, image/jpeg");
            fileInput.classList.add("ql-image");
            // 监听选择文件
            fileInput.addEventListener("change", function () {
                console.log(fileInput.files[0]);
                let file = fileInput.files[0];
                if (!/image\/\w+/.test(file.type)) {
                    console.log("图片格式不正确");
                    return false;
                }
                let reader = new FileReader();
                reader.readAsDataURL(file);
                reader.onload = function () {
                    let img = new Image();
                    img.src = this.result;
                    img.onload = function () {
                        let that = this;
                        let scale = 800 / that.width;
                        let w = 800;
                        let h = that.height * scale;
                        // let w = that.width;
                        // let h = that.height;
                        let canvas = document.createElement("canvas");
                        let ctx = canvas.getContext("2d");
                        let anw = document.createAttribute("width");
                        anw.nodeValue = w;
                        let anh = document.createAttribute("height");
                        anh.nodeValue = h;
                        canvas.setAttributeNode(anw);
                        canvas.setAttributeNode(anh);
                        ctx.drawImage(that, 0, 0, w, h);
                        let base64 = canvas.toDataURL("image/jpeg", 0.5);
                        // let length = self.quill.getSelection(true).index;
                        // self.quill.insertEmbed(length, "image", base64);
                        // self.quill.setSelection(length + 1);
                    };
                };
                // 创建formData
                var formData = new FormData();
                formData.append(uploadConfig.name, fileInput.files[0]);
                // formData.append("object", "product");
                // 图片上传
                var xhr = new XMLHttpRequest();
                xhr.open(uploadConfig.methods, uploadConfig.action, true);
                // 上传数据成功，会触发
                xhr.onload = function (e) {
                    console.log(xhr.status);
                    if (xhr.status === 200) {
                        var res = JSON.parse(xhr.responseText);
                        let length = self.quill.getSelection(true).index;
                        //这里很重要，你图片上传成功后，img的src需要在这里添加，res.path就是你服务器返回的图片链接。
                        self.quill.insertEmbed(length, "image", res.value);
                        self.quill.setSelection(length + 1);
                    }
                    fileInput.value = "";
                };
                // 开始上传数据
                xhr.upload.onloadstart = function (e) {
                    console.log(e)
                    console.log(fileInput.value);
                    fileInput.value = "";
                };
                // 当发生网络异常的时候会触发，如果上传数据的过程还未结束
                xhr.upload.onerror = function (e) { };
                // 上传数据完成（成功或者失败）时会触发
                xhr.upload.onloadend = function (e) {
                    // console.log('上传结束')
                };
                xhr.send(formData);
            });
            this.container.appendChild(fileInput);
        }
        fileInput.click();
    },
    dealImage: function (path, obj, callback) {
        var img = new Image();
        img.src = path;
        img.onload = function () {
            var that = this;
            // 默认按比例压缩
            var w = that.width,
                h = that.height,
                scale = w / h;
            w = obj.width || w;
            h = obj.height || w / scale;
            var quality = obj.quality || 0.7; // 默认图片质量为0.7
            //生成canvas
            var canvas = document.createElement("canvas");
            var ctx = canvas.getContext("2d");
            // 创建属性节点
            var anw = document.createAttribute("width");
            anw.nodeValue = w;
            var anh = document.createAttribute("height");
            anh.nodeValue = h;
            canvas.setAttributeNode(anw);
            canvas.setAttributeNode(anh);
            ctx.drawImage(that, 0, 0, w, h);
            // 图像质量
            if (obj.quality && obj.quality <= 1 && obj.quality > 0) {
                quality = obj.quality;
            }
            // quality值越小，所绘制出的图像越模糊
            var base64 = canvas.toDataURL("image/jpeg", quality);
            // 回调函数返回base64的值
            callback(base64);
        };
    },
};

